package com.github.nuggets.entity;


import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.HashSet;

/**
 * @description:
 * @author: wangsj
 * @date: 2022/3/15 7:17 下午
 */
@Data
public class AuthenticAccount extends Account implements UserDetails {

    public AuthenticAccount(Account origin) {

        BeanUtils.copyProperties(origin, this);

        String[] roles = origin.getRole().split(",");
        for (String role : roles) {
            authorities.add(new SimpleGrantedAuthority(role));
        }
    }

    /**
     * 该用户拥有的授权，譬如读取权限、修改权限、增加权限等等
     */
    private Collection<GrantedAuthority> authorities = new HashSet<>();


    @Override
    public String getUsername() {
        return this.getUserName();
    }

    /**
     * 账号是否过期
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 是否锁定
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 密码是否过期
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 是否被锁定
     */
    @Override
    public boolean isEnabled() {
        return true;
    }
}
